package org.wyjc.gap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Gap {
	private Page page = new Page();

	public Map<String, List<CssSelector>> elements(String content, String selectionText) {
		Document doc = Jsoup.parse(content);
		List<Element> es = doc.select("a");
		List<CssSelector> list = new ArrayList<CssSelector>();
		for (Element e : es) {
			if (e.hasText()) {
				// System.out.println(e.text());
				// System.out.println(e.cssSelector());
				CssSelector cs = new CssSelector();
				cs.setSelector(e.cssSelector());
				cs.setText(e.text());
				cs.setSize(e.cssSelector().split(">").length);
				cs.setHref(e.attr("href"));
				list.add(cs);
			}
		}
		page.setList(list);
		page.handlePage();
		return page.getIsizeMap();
	}

	public List<CssSelector> parseContent(String content, String selectionText) {
		Document doc = Jsoup.parse(content);
		List<Element> es = doc.getAllElements();
		List<CssSelector> list = new ArrayList<CssSelector>();
		for (Element e : es) {
			if (e.hasText()) {
				CssSelector cs = new CssSelector();
				cs.setSelector(e.cssSelector());
				cs.setText(e.text());
				// cs.setSize(e.cssSelector().split(">").length);
				// cs.setHref(e.attr("href"));
				list.add(cs);
			}
		}
		page.setList(list);
		return page.parseContent(selectionText);
	}
	public List<CssSelector> parseContent(String content) {
		Document doc = Jsoup.parse(content);
		List<Element> es = doc.getAllElements();
		List<CssSelector> list = new ArrayList<CssSelector>();
		for (Element e : es) {
			if (e.hasText()) {
				CssSelector cs = new CssSelector();
				cs.setSelector(e.cssSelector());
				cs.setText(e.text());
				// cs.setSize(e.cssSelector().split(">").length);
				// cs.setHref(e.attr("href"));
				list.add(cs);
			}
		}
		page.setList(list);
		return page.getList();
	}
	public static void main(String[] args) throws IOException {
		Gap g = new Gap();
		Document doc = Jsoup
				.connect(
						"http://qy.58.com/38370716279060/?psid=159317188195679644579126507&entinfo=25532781739853_j&PGTID=0d302f82-0000-2621-cf28-741efb08bc1b&ClickID=6")
				.userAgent("Mozilla").timeout(8000).get();
		// List<Element> es = doc.select("a");
		List<Element> es = doc.getAllElements();
		List<CssSelector> list = new ArrayList<CssSelector>();
		for (Element e : es) {
			if (e.hasText()) {
				System.out.println(e.text());
				System.out.println(e.cssSelector());
				CssSelector cs = new CssSelector();
				cs.setSelector(e.cssSelector());
				cs.setText(e.text());
				// cs.setSize(e.cssSelector().split(">").length);
				// cs.setHref(e.attr("href"));
				list.add(cs);
			}
		}
		g.getPage().setList(list);
		// g.getPage().handlePage();
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
